package com.rf.richfitwheel.generate;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

public class Generator {

    public static void main(String[] args) {

        //全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        //D:\work\temp\generate
//        globalConfig.setOutputDir(System.getProperty("user.dir") + "/src/main/java/com/rf/richfitwheel/ent")
        globalConfig.setOutputDir("D:\\mybatis-code")
                .setAuthor("lihong")
                .setOpen(false) //设置生成后是否自动打开目录
                .setFileOverride(true)  //设置文件存在时是否覆盖
                .setControllerName("%sController")
                .setServiceName("%sService")    //设置Service接口名后缀
                .setServiceImplName("%sServiceImpl")    //设置接口名后缀
                .setMapperName("%sMapper")
                .setXmlName("%sMapper")
                .setIdType(IdType.ASSIGN_UUID)  //设置主键生成策略
                .setSwagger2(true);     //设置是否生成swagger注解

        //数据源配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL)
                .setDriverName("com.mysql.cj.jdbc.Driver")
                .setUrl("jdbc:mysql://192.168.137.211:4000/bill?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=CTT&zeroDateTimeBehavior=convertToNull&nullCatalogMeansCurrent=true")
                .setUsername("root")
                .setPassword("1qaz2wsx");

        //策略配置
        StrategyConfig strategyConfig = new StrategyConfig();
//        strategyConfig.setInclude("user", "order")  //指定需要生成代码的表名
        strategyConfig.setInclude("accept_bill_info")  //指定需要生成代码的表名
//                .setExclude("要排除的表") //排除特定的表
                .setNaming(NamingStrategy.underline_to_camel)       //设置表名转类名策略
                .setColumnNaming(NamingStrategy.underline_to_camel) //设置列名转属性名策略
                .setEntityBooleanColumnRemoveIsPrefix(true) //移除boolean类型字段的is前缀
//                .setControllerMappingHyphenStyle(true)  //驼峰转连接字符
                .setEntityLombokModel(true)     //设置实体类使用lombok模型
                .setRestControllerStyle(true);   //设置controller使用result风格
//                .setTablePrefix(new String[]{"tbl_"});  //设置表名前缀

        //包配置
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent("com.rf.richfitwheel")  //设置父包名
                .setModuleName("outbill")   //设置父包模块名
                .setController("controller")    //设置控制器类所在的子包名
                .setService("service")  //
                .setServiceImpl("service.impl") //
                .setEntity("model") //
                .setMapper("dao")   //
                .setXml("mapper");  //

        //模板配置
        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setXml(null)
                .setController("templates/controller.java.vm");  //设置controller模板路径
//                .setEntity("")  //设置实体类模板路径
//                .setMapper(""); //设置Mapper接口模板路径

        AutoGenerator autoGenerator = new AutoGenerator();
        autoGenerator.setGlobalConfig(globalConfig)
                .setDataSource(dataSourceConfig)
                .setStrategy(strategyConfig)
                .setPackageInfo(packageConfig)
                .setTemplate(templateConfig);
        autoGenerator.execute();
    }
}
